<?php
$keys = array();

$first = mktime(12, 0, 0, date("m"), date("d"), date("Y"));

for ($i = 0; $i < 10; $i++) {
    $date = $first - $i * 24 * 3600;
    $key = array(
        "type" => 'day',
        "day" => date("d", $date),
        "month" => date("m", $date),
        "year" => date("Y", $date),
        "description" => $GLOBALS['days_short']['sk'][date("w", $date)] . " " . date("d", $date) . "." . date("m", $date) . "."
    );
    $keys[] = $key;
}

$first = mktime(12, 0, 0, date("m"), 15, date("Y"));

for ($i = 0; $i < 10; $i++) {
    $date = $first - $i * 30 * 24 * 3600;
    $key = array(
        "type" => 'month',
        "month" => date("m", $date),
        "year" => date("Y", $date),
        "description" => date("m", $date) . "/" . date("Y", $date)
    );
    $keys[] = $key;
}

$GLOBALS['template_data']['keys'] = $keys;

$record_obj = new record_obj();
$user_obj = new user_obj();
$filter = array(
    'userlevel' => USER_TYPE_TECHNIC,
    'active' => 1
);
        
$users = $user_obj->getUsers('name, surname, users_id', $filter);   

$output = array();
$sums = array();

foreach ($keys as $j => $key) {
    $sums[$key['description']] = array(
        'with' => 0,
        'without' => 0
    );
}

foreach ($users as $i => $user) {
    $row = array();
    $row['user'] = $user;
    $row['values'] = array();
    foreach ($keys as $j => $key) {
        $temp = array();
        $temp['type'] = $key['type'];
        
        $filter = array(
            'type' => 1,
            'user_id' => $user['users_id'],
        );
        if ($key['type'] == 'day'){
            $filter['date'] = $key['year'] . '-' . $key['month'] . '-' . $key['day'];
        } else {
            $filter['monthm'] = $key['month'];
            $filter['year'] = $key['year'];
        }
        
        $temp['filter'] = urlencode(base64_encode(json_encode($filter)));
        
        $records = $record_obj->getListOfRecords($filter);
        $temp['withAC'] = 0;
        $temp['withoutAC'] = 0;
        if (!empty($records)){
            foreach ($records as $k => $record) {
                $temp['withAC'] += $record['amount'];
                $sums[$key['description']]['with'] += $record['amount']; 
                if ($record['client'] != 11){
                    $temp['withoutAC'] += $record['amount'];
                    $sums[$key['description']]['without'] += $record['amount']; 
                }
            }
        }
        
        $row['values'][$key['description']] = $temp;
    }
    $output[] = $row;
}

$GLOBALS['template_data']['output'] = $output;
$GLOBALS['template_data']['sums'] = $sums;
?>
